package com.zdlog.wenjuan.controller.web;

import com.zdlog.wenjuan.VO.ResultBoolenVO;
import com.zdlog.wenjuan.constant.CookieConstant;
import com.zdlog.wenjuan.constant.RedisConstant;
import com.zdlog.wenjuan.dataobject.FormInfo;
import com.zdlog.wenjuan.dataobject.UserInfo;
import com.zdlog.wenjuan.enums.FormStatusEnum;
import com.zdlog.wenjuan.service.FormService;
import com.zdlog.wenjuan.service.UserService;
import com.zdlog.wenjuan.util.CookieUtil;
import com.zdlog.wenjuan.util.KeyUtil;
import com.zdlog.wenjuan.util.ResultBoolenVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @program: wenjuanSer
 * @PACKAGE_NAME: com.zdlog.wenjuan.controller
 * @author: Kuaile
 * @create: 2018-02-04 17:08
 **/

@Controller
@RequestMapping("/form")
@ResponseBody
@Slf4j
public class FormInfoController {


    @Autowired
    private FormService formService;


    @Autowired
    private UserService service;


    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/list")
    public ModelAndView list(HttpServletRequest request,
                             Map<String, Object> map,
                             @RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size) {
        //查询cookie
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        //去redis里查询
        String tokenValue = redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()));
        UserInfo userInfo = service.findUserId(tokenValue);
        PageRequest pageRequest = new PageRequest(page - 1, size);
        map.put("currentPage", page);
        map.put("userId", tokenValue);
        map.put("size", size);
        map.put("icon", userInfo.getIcon());
        map.put("formList", formService.list(tokenValue, pageRequest));
        return new ModelAndView("form/list");
    }


    @PostMapping("/toStatus")
    public ResultBoolenVO doStatus(@RequestParam(value = "id") String id,
                                   @RequestParam(value = "status") String status) {
        if (status.equals(FormStatusEnum.STOP.getMessage())) {
            FormInfo formInfo = formService.findId(id);
            formInfo.setStatus(FormStatusEnum.START.getCode());
            FormInfo result = formService.updata(formInfo);
            if (result != null) {
                return ResultBoolenVOUtil.success();
            }
        } else if (status.equals(FormStatusEnum.START.getMessage())) {
            FormInfo formInfo = formService.findId(id);
            formInfo.setStatus(FormStatusEnum.STOP.getCode());
            FormInfo result = formService.updata(formInfo);
            if (result != null) {
                return ResultBoolenVOUtil.success();
            }
        }
        return ResultBoolenVOUtil.error("修改失败");

    }

    @GetMapping("/add")
    public ModelAndView add() {
        return new ModelAndView("form/add");
    }


    @PostMapping("/toAdd")
    public ResultBoolenVO toAdd(HttpServletRequest request, @RequestParam(value = "formname") String formname) {
        //查询cookie
        Cookie cookie = CookieUtil.get(request, CookieConstant.TOKEN);
        //去redis里查询
        String tokenValue = redisTemplate.opsForValue().get(String.format(RedisConstant.TOKEN_PREFIX, cookie.getValue()));
        FormInfo formInfo = new FormInfo();
        formInfo.setFormName(formname);
        formInfo.setUserId(tokenValue);
        formInfo.setStatus(new Integer(FormStatusEnum.STOP.getCode()));
        formInfo.setFormId(KeyUtil.genUniqueKey());
        FormInfo result = formService.save(formInfo);
        if (result != null)
            return ResultBoolenVOUtil.success();

        return ResultBoolenVOUtil.error("创建失败");
    }


    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam(value = "id") String formId, Map<String, Object> map) {
        ResultBoolenVO resultBoolenVO = formService.detail(formId);
        if (resultBoolenVO.getOk()) {
            map.put("form", resultBoolenVO.getData());
            return new ModelAndView("form/detail");
        }
        map.put("msg", "无问题");
        map.put("url", "/form/list");
        return new ModelAndView("common/error");

    }


    @PostMapping("/doDelete")
    public ResultBoolenVO doDelete(@RequestParam(value = "id") String id,
                                   @RequestParam(value = "status") String status) {
        if (status.equals(FormStatusEnum.START.getMessage())) {
            return ResultBoolenVOUtil.error("请停止答卷，再删除");
        }
        ResultBoolenVO resultBoolenVO = formService.delete(id);
        return resultBoolenVO;
    }




}
